package com.zzw.utils;

import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 赵志伟
 * @version 1.0
 */
@SuppressWarnings({"all"})
public class WebUtils {
    public static final String TCHATGE_BOX_IMG_DIRECTORY = "img/";

    private static final char SEPARATOR = '_';
    /**
     * 下划线
     */

    public static ThreadLocal<HttpServletRequest> servletRequest = new ThreadLocal<>();

    public static ThreadLocal<HttpSession> session = new ThreadLocal<>();


    public static String getYearMonthDay() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd/");
        String format = sdf.format(date);
        return format;// 2024/12/11
    }

        public static String getOrderBy(String orderByColumn, String isAsc)
        {
            if (StringUtils.isEmpty(orderByColumn))
            {
                return "";
            }
            if (StringUtils.isEmpty(isAsc))
            {
                return "";
            }
            return toUnderScoreCase(orderByColumn) + " " + isAsc;
        }

    public static void main(String[] args) {
        System.out.println(toUnderScoreCase("to_number(create_time)"));
    }
    /**
     * 驼峰转下划线命名
     */
    public static String toUnderScoreCase(String str)
    {
        if (str == null)
        {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        // 前置字符是否大写
        boolean preCharIsUpperCase = true;
        // 当前字符是否大写
        boolean curreCharIsUpperCase = true;
        // 下一字符是否大写
        boolean nexteCharIsUpperCase = true;
        for (int i = 0; i < str.length(); i++)
        {
            char c = str.charAt(i);
            if (i > 0)
            {
                preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1));
            }
            else
            {
                preCharIsUpperCase = false;
            }

            curreCharIsUpperCase = Character.isUpperCase(c);

            if (i < (str.length() - 1))
            {
                nexteCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1));
            }

            if (preCharIsUpperCase && curreCharIsUpperCase && !nexteCharIsUpperCase)
            {
                sb.append(SEPARATOR);
            }
            else if ((i != 0 && !preCharIsUpperCase) && curreCharIsUpperCase)
            {
                sb.append(SEPARATOR);
            }
            sb.append(Character.toLowerCase(c));
        }

        return sb.toString();
    }
}
